package com.ce.pms.system.custservice.service.impl;

import java.util.List;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.ICustEventNatureDao;
import com.ce.pms.system.custservice.model.CustEventNatureDO;
import com.ce.pms.system.custservice.service.ICustEventNatureService;
import com.ce.pms.system.custservice.vo.CustEventNatureVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 描述：事件性质service实现
 * @author 王浩羽
 *
 */
public class CustEventNatureServiceImpl extends AbstractCustomerEventServiceImpl<CustEventNatureVO, CustEventNatureDO> implements ICustEventNatureService
{
	private ICustEventNatureDao custEventNatureDao;
	
	public boolean addCustEventNature(CustEventNatureVO eventNatureVO)
	{
		if(!BlankUtil.isBlank(eventNatureVO) && !BlankUtil.isBlank(eventNatureVO.getName()))
		{
			if(isNameExisit(eventNatureVO.getName())){
				throw new BusinessException("custservice:system_name_exist_exception",BusinessException.WARNING);
			}
			CustEventNatureDO eventNatureDO = assembleV2D(eventNatureVO,null);
			eventNatureDO.setSysInfo(createInfo(eventNatureVO.getCreator()));
			return custEventNatureDao.saveObject(eventNatureDO) != null;
		}
		return false;
	}
	
	
	public boolean deleteCustEventNatureById(Long eventNatureId)
	{
		if(eventNatureId==null)
		{
			return false;
		}
		try
		{
			custEventNatureDao.deleteObjectById(eventNatureId);
			return true;
		}
		catch(RuntimeException e)
		{	//如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("custservice:system_delete_inused_exception",BusinessException.WARNING);
		}
	}
	
	public boolean editCustEventNature(CustEventNatureVO eventNatureVO)
	{
		if(!BlankUtil.isBlank(eventNatureVO) && !BlankUtil.isBlank(eventNatureVO.getKeyword()))
		{
			CustEventNatureDO eventNatureDO = custEventNatureDao.findById(eventNatureVO.getKeyword());
			if(eventNatureDO==null)
			{
				return false;
			}
			eventNatureDO.setRemark(eventNatureVO.getRemark());
			eventNatureDO.setSequence(eventNatureVO.getSequence());
			//不能修改名称，VO与DO的名称应该是一致的，这里默认做名称置换操作
			eventNatureDO.setName(eventNatureVO.getName());
			modifyInfo(eventNatureDO.getSysInfo(), eventNatureVO.getModifier());
			return custEventNatureDao.updateObject(eventNatureDO) != null;
		}
		return false;
	}
	
	protected boolean isNameExisit(String name){
		return custEventNatureDao.findUniqueByName(name)!=null;
	}
	
	public Page queryCustEventNatureListByPage(SearchVO searchVO)
	{
		return queryVOListByPage(searchVO);
	}
	
	
	
	public void setCustEventNatureDao(ICustEventNatureDao custEventNatureDao)
	{
		this.custEventNatureDao = custEventNatureDao;
	}
	

	@SuppressWarnings("unchecked")
	public List<CustEventNatureVO> queryAllCustEventNature()
	{
		List<CustEventNatureDO> custEventNatureDOList = custEventNatureDao.findAll();
		sortList(custEventNatureDOList);
		return assembleDOList2VOList(custEventNatureDOList);
	}


	@Override
	protected int compareDO(CustEventNatureDO o1, CustEventNatureDO o2)
	{
		int s1 = Integer.MAX_VALUE;
		int s2 = Integer.MAX_VALUE;
		if(o1.getSequence() != null)
		{
			s1 = o1.getSequence();
		}
		if(o2.getSequence() != null)
		{
			s2 = o2.getSequence();
		}
		return s1 - s2;
	}


	@Override
	protected CustEventNatureDO findObjectDOById(Long id)
	{
		return custEventNatureDao.findById(id);
	}


	@SuppressWarnings("unchecked")
	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{
		Page page =  custEventNatureDao.queryByPage(searchVO);
		List<CustEventNatureDO> list = page.getResult();
		sortList(list);
		page.setResult(list);
		return page;
	}
	
	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "custservice:system_name_exist_exception";
	}
}
